<%
	//->Template对象
	var Template=Class.create
	(
		{
			initialize:function($template,$pattern) 
			{
				this.template=$template.toString();
				this.pattern=$pattern||Template.Pattern;
			},	
			evaluate:function($object) 
			{
				if(Object.isFunction($object.toTemplateReplacements))
				{
					$object=$object.toTemplateReplacements();
				}				
				return this.template.gsub
				(
					this.pattern,
					function(match) 
					{
						if($object==null) return '';
						
						var before=match[1]||'';
						if(before=='\\') return match[2];
						
						var ctx=$object,expr=match[3];
						var pattern=/^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/,match=pattern.exec(expr);
						if(match == null) return before;
						
						while(match!=null) 
						{
							var comp=match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1];
							ctx=ctx[comp];
							if(null==ctx||''== match[3]) break;
							expr=expr.substring('[' == match[3] ? match[1].length : match[0].length);
							match=pattern.exec(expr);
						}						
						return before+String.interpret(ctx);
					}.bind(this)
				);
			}
		}
	);
	Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;	// /(^|.|\r|\n)(\<%=\s*(\w+)\s*%\>)/
	
//关于自定义pattern的例子
//var syntax = /(^|.|\r|\n)(\<%=\s*(\w+)\s*%\>)/; //matches symbols like '<%王洋= field %王洋>'
//var t = new Template('<div>Name: <b><王洋%= name %王洋></b>, Age: <b><王洋%=age%王洋></b></div>', syntax);
//t.evaluate( {name: 'John Smith', age: 26} ); // -> <div>Name: <b>John Smith</b>, Age: <b>26</b></div>
%>